Page({
    data: {
        dataList: {}, // 存储咨询师详情数据
        baseUrl: 'http://localhost:8080',
        value: 3,
        subscribeConsultTime: "", 
        situationDescription: "", 
        counselorId: 0,
        userId: null  // 初始化为null，后续从storage获取
    },

    onLoad(options) {
        // 1. 获取缓存中的用户ID
        const userId = wx.getStorageSync('userId');
        if (userId) {
            this.setData({
                userId: userId
            });
        } else {
            // 如果没有用户ID，提示登录并跳转到登录页
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            });
            setTimeout(() => {
                wx.navigateTo({
                    url: '/pages/login/login'  // 替换为你的登录页路径
                });
            }, 1500);
            return; // 终止后续逻辑
        }

        // 2. 获取咨询师ID（从跳转参数中）
        console.log("接收的咨询师ID参数：", options.id);
        if (options.id) {
            this.setData({
                counselorId: parseInt(options.id, 10) 
            });
        } else {
            wx.showToast({
                title: '未获取到咨询师信息',
                icon: 'none'
            });
            return;
        }

        // 3. 初始化默认日期
        const today = new Date();
        const year = today.getFullYear();
        const month = (today.getMonth() + 1).toString().padStart(2, '0');
        const day = today.getDate().toString().padStart(2, '0');
        this.setData({
            subscribeConsultTime: `${year}-${month}-${day}`
        });

        // 4. 请求咨询师详情
        this.getCounselorDetail();
    },

    // 获取咨询师详情
    getCounselorDetail() {
        const {
            counselorId,
            baseUrl
        } = this.data;
        console.log("请求详情的咨询师ID：", counselorId);

        if (!counselorId || counselorId === 0) {
            wx.showToast({
                title: '参数错误，无法获取咨询师信息',
                icon: 'none'
            });
            return;
        }

        wx.request({
            url: `${baseUrl}/counselor/${counselorId}`,
            method: 'GET',
            success: (resp) => {
                console.log("咨询师详情接口返回：", resp.data);
                if (resp.data.code === 200 && resp.data.data) {
                    this.setData({
                        dataList: resp.data.data
                    });
                } else {
                    wx.showToast({
                        title: '获取详情失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error("详情请求失败：", err);
                wx.showToast({
                    title: '网络错误，无法获取详情',
                    icon: 'none'
                });
            }
        });
    },

    // 日期选择器变化事件
    onDateChange(e) {
        this.setData({
            subscribeConsultTime: e.detail.value
        });
    },

    // 情况说明输入事件
    onDescInput(e) {
        this.setData({
            situationDescription: e.detail.value
        });
    },

    // 取消预约
    onCancel() {
        wx.navigateBack();
    },

    // 确认预约（提交数据到后端）
    onConfirm() {
        const {
            userId,
            counselorId,
            subscribeConsultTime,
            situationDescription,
            baseUrl
        } = this.data;

        // 再次验证用户ID（防止异常情况）
        if (!userId) {
            wx.showToast({
                title: '用户信息异常，请重新登录',
                icon: 'none'
            });
            return;
        }

        // 验证咨询师ID
        if (!counselorId || counselorId === 0) {
            wx.showToast({
                title: '咨询师信息异常',
                icon: 'none'
            });
            return;
        }

        // 验证预约时间
        if (!subscribeConsultTime) {
            wx.showToast({
                title: '请选择预约时间',
                icon: 'none'
            });
            return;
        }

        // 打印提交的参数用于调试
        console.log("提交的预约参数：", {
            userId: userId,
            counselorId: counselorId,
            subscribeConsultTime: subscribeConsultTime,
            situationDescription: situationDescription
        });

        // 提交预约请求
        wx.request({
            url: `${baseUrl}/subscribeConsult`,
            method: 'POST',
            data: {
                userId: userId,
                counselorId: counselorId,
                subscribeConsultTime: subscribeConsultTime,
                situationDescription: situationDescription
            },
            success: (resp) => {
                console.log("预约接口返回结果：", resp.data);
                if (resp.data.code === 200) {
                    wx.showToast({
                        title: '预约成功',
                        icon: 'success'
                    });
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 1500);
                } else {
                    wx.showToast({
                        title: resp.data.message || '预约失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error("预约请求失败：", err);
                wx.showToast({
                    title: '网络错误，预约失败',
                    icon: 'none'
                });
            }
        });
    }
});
